<?php

namespace Xx2025\Kingdee\Erp\Service;

use Couchbase\BaseException;
use Xx2025\Kingdee\Erp\BaseResponse;
use Xx2025\Kingdee\Erp\Client;
use Xx2025\Kingdee\Erp\Response\GyErpItemsGet;
use Xx2025\Kingdee\Erp\Response\GyErpPurchaseArriveAdd;
use Xx2025\Kingdee\Erp\Response\GyErpPurchaseArriveGet;

class Goods {




    /**创建商品以及SKU
     * @param $goodsCode  //商品编号
     * @param $goodsName  //商品名称
     * @param $skuList   //SKU列表
     * @param $itemAddAttribute  //0:普通商品  1:唯一码商品   2:批次商品   默认0
     * @param $categoryCode //商品类别
     * @param $extraParams //额外参数
     */
    public static function newGoodsAndSku(string $goodsCode, string $goodsName, array $skuList, string $categoryCode, int $itemAddAttribute, $extraParams = []) :BaseResponse
    {

        $goodsName = preg_replace('/[^\p{Han}a-zA-Z0-9]/u', '', $goodsName);

        $params = [
            'code'=>$goodsCode,
            'name'=>$goodsName,
            'category_code'=>$categoryCode,
            'item_add_attribute'=>$itemAddAttribute,
            'skus'=>$skuList
        ];
        foreach ($extraParams as $key => $value) {
            $params[$key] = $value;
        }

        $res = Client::request('gy.erp.item.add', $params);


        return $res;
    }

    /**更新商品以及SKU
     * @param $goodsCode
     * @param $goodsName
     * @param $skuList
     * @param $categoryCode
     * @param $itemAddAttribute
     * @param $extraParams
     */
    public static function updateGoodsAndSku(string $goodsCode, string $goodsName, array $skuList, string $categoryCode, int $itemAddAttribute, $extraParams = []) :BaseResponse
    {
      

        $goodsName = preg_replace('/[^\p{Han}a-zA-Z0-9]/u', '', $goodsName);
        $params = [
            'code'=>$goodsCode,
            'name'=>$goodsName,
            'category_code'=>$categoryCode,
            'item_add_attribute'=>$itemAddAttribute,
            'skus'=>$skuList
        ];
        foreach ($extraParams as $key => $value) {
            $params[$key] = $value;
        }
        $res = Client::request('gy.erp.item.update', $params);
        return $res;
    }

    /**查询商品
     * @param string $goodsCode
     */
    public static function queryGoodsByCode(string $goodsCode) :GyErpItemsGet
    {

        $params = [
            'code'=>$goodsCode,
            'combine' => false,
        ];

        $response = GyErpItemsGet::getInstance();

        Client::request('gy.erp.items.get', $params, $response);  //GyErpItemsGet


        if(!$response->isSuccessful()){
            return $response;
        }


        $goodsInfo = $response->getResponseContents()['items'][0] ?? [];
        if(empty($goodsInfo)) return $response;

        $skuList = [];
        foreach ($goodsInfo['skus'] as $erpSku){
            $skuList[] = $erpSku['code'];
        }

        $response->setSkuList($skuList);
        return $response;

    }


    /**创建商品条形码
     * @param string $barCode
     * @param string $goodsCode
     * @param string $skuCode
     * @return BaseResponse
     * @throws \Throwable
     */
    public static function newGoodsBarcode(string $barCode, string $goodsCode, string $skuCode = ''):BaseResponse
    {
        $params = [
            'item_code'=>$goodsCode,
            'barcode'=>$barCode,
        ];
        if($skuCode){
            $params['sku_code'] = $skuCode;
        }

        $response = Client::request('gy.erp.item.barcode.add', $params);
        return $response;
    }


    /**商品采购入库并审核
     * @param string $orderNo 订单号
     * @param string $supplierCode  供应商代码
     * @param string $warehouseCode 仓库代码
     * @param array $erpDetailList  商品明细 示例：[['item_code'=>'', 'sku_code'=>'', 'qty'=>'', 'uniqueCode'=>'']]
     * @param array $extraParams  额外参数
     */
    public static function newGoodsPurchase(string $orderNo, string $supplierCode, string $warehouseCode, array $erpDetailList, $extraParams = []) :GyErpPurchaseArriveAdd
    {

        $params = [
            'wms_bizcode'=>$orderNo,
            'warehouse_code'=>$warehouseCode,
            'supplier_code'=>$supplierCode,
            'detail_list'=>$erpDetailList,
        ];

        foreach ($extraParams as $key => $value){
            $params[$key] = $value;
        }

        $response = GyErpPurchaseArriveAdd::getInstance();

        Client::request('gy.erp.purchase.arrive.add', $params, $response);

        if(!$response->isSuccessful()){
            return $response;
        }
        $billCode = $response->getResponseContents()['code'];
        $response->setBillCode($billCode);
        return $response;
    }




    /**查询采购入库订单
     * @param string $billCode
     * @return GyErpPurchaseArriveGet
     * @throws \Throwable
     */
    public static function queryGoodsPurchaseOrder(string $billCode):GyErpPurchaseArriveGet
    {
        $requestBody = [
            'code'=>$billCode,
        ];

        $response = GyErpPurchaseArriveGet::getInstance();

        Client::request('gy.erp.purchase.arrive.get', $requestBody, $response);

        $data = $response->getResponseContents();


        if(!isset($data['purchaseArrives'])){
            return $response;
        }
        $info = null;
        foreach ($data['purchaseArrives'] as $item){
            if($item['code'] == $billCode){
                $info = $item;
                break;
            }
        }

        if(isset($info['status']) && $info['status'] == 1){
            $response->setPurchaseState(true);
            return $response;
        }
        return $response;
    }

}